// pages/ziliao/ziliao.js
import {ytdetail,sxdetail,gtypes,getshi, getyslist,getziqu,getlqway,ziliaop} from '../../utils/api'
import { areaList } from '@vant/area-data';
Page({

  /**
   * 页面的初始数据
   */
  data: {
      yid:1,
      sid:5,
      yt:{},
      sx:{},
      types:[],
      dtype:{},
      shiming:{},
      show:false,
      areaList:areaList,
      ashow:false,
      address:"请选择",
      fshow:false,
      fdata:[{name:1},{name:2},{name:3},{name:4},{name:5},{name:6},{name:7},{name:8},{name:9},{name:10}],
      fen:1,
      yslist:[],
      lqshow:false,
      ziqu:{},
      zqbox:false,
      yjbox:false,
      dybox:false,
      lqway:[],
      lqdata:[],
      xyj:false,
      zq:{name:'',tel:''},
      yj:{name:'',tel:'',addr:''},
      dy:{email:''},
      lqtdata:{},
      price:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var yid=options.yid
    var sid=options.sid
    
    var that=this
    that.setData({
      yid:yid,
      sid:sid
    })
    ytdetail(that.data.yid).then((ret)=>{
       that.setData({
        yt:ret.data
       })
    })
    sxdetail(that.data.sid).then((ret1)=>{
      that.setData({
        sx:ret1.data,
        price:ret1.data.price
      })
   })
   gtypes().then((ret2)=>{
      that.setData({
        types:ret2.data,
       dtype:ret2.data[0]
      })
   })
   getshi().then((ret3)=>{
      
     if(ret3.code==1){
      that.setData({
       shiming:ret3.msg
      })
     }else{
       wx.redirectTo({
         url: '/pages/shiming/shiming',
       })
     }
   })
   getyslist(that.data.sid).then((ret4)=>{
    var that=this
    that.setData({
      yslist:ret4.data
    })
   })
   getziqu().then((ret5)=>{
    var that=this
    that.setData({
      ziqu:ret5.data
    })
   })
   
   getlqway().then((ret6)=>{
     console.log(ret6)
    var that=this
    that.setData({
      lqway:ret6.data
    })
   })
  },
  stype(){
    var that=this
    that.setData({
      show:true
    })
  },
  closetype(){
    var that=this
    that.setData({
      show:false
    })
  },
  onSelectf(e){
    var that=this;
    var fdata=that.data.fdata;
    for(var i=0;i< fdata.length;i++){
      fdata[i].color="#000"
    }
    var price=that.data.sx.price;
    price=parseFloat(price)+parseFloat((e.detail.name-1)*that.data.sx.add_price);
    console.log(price)
    fdata[e.detail.name-1].color="#1989fa"
    that.setData({
      fen:e.detail.name,
      fshow:false,
      fdata:fdata,
      price:price
    })
  },
  onSelect(e){
    var that=this;
    var types=that.data.types;
    for(var i=0;i< types.length;i++){
      types[i].color="#000"
    }
    types[e.detail.id].color="#1989fa"
    that.setData({
      dtype:e.detail,
      show:false,
      types:types
    })
  },
  area_top(){
    this.setData({ ashow: true });
  },
  onClose(){
    this.setData({ ashow: false});
  },
  achaange(e){
    var that=this;
    that.setData({
      address:e.detail.values[0].name+","+e.detail.values[1].name,
      aids:e.detail.values[0].code+","+e.detail.values[1].code,
      ashow: false
    })
  },
  lqway(){
    this.setData({
      lqshow:true
    })
  },
  closelq(){
    this.setData({
      lqshow:false
    })
  },
  lq(e){
    var mc=e.detail+'box'
    var that=this
    var lqway=that.data.lqway
    var lqdata={}
    for(var i=0;i<lqway.length;i++){
      if(lqway[i].ids==e.detail){
        lqdata=lqway[i];
        break;
      }
    }
    that.setData({
      zqbox:false,
      yjbox:false,
      dybox:false,
      [mc]:true,
      radio:e.detail,
      lqdata:lqdata
    })
     
  },
  qx(){this.setData({lqshow:false})},
  qrlq(){
       var that=this
       var mc=this.data.lqdata.ids
       var lqtdata=that.data[mc]
       var lt=1;
      for(var p in lqtdata){
        if(lqtdata[p]===''){
           wx.showToast({
             title: '请填写信息',
             icon:'none',
           })
           lt=0;
           break;
        }
      }
      if(lt==1){
        that.setData({
          lqtdata:lqtdata,
          lqshow:false
        })
      }
  },
  pdchange(e){
    var that=this
    var arr=e.currentTarget.dataset.ids
    var lqp=that.data[arr]
    lqp[e.currentTarget.dataset.name]=e.detail
     that.setData({
       [arr]:lqp
     })
  },
  xzdz(){
    wx.navigateTo({
      url: '/pages/addr/addr',
    })
  },
  fenshu(){
    this.setData({fshow:true})
  },
  closef(){
    this.setData({fshow:false})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  zipt(){
    var that=this;
    console.log(that.data)
    if(that.data.dtype.id==0){
      wx.showToast({
        title: '请选择公证类别',
        icon:'none'
      })
      return;
    }
    if(Object.keys(that.data.lqtdata).length==0){
      wx.showToast({
        title: '请填写领取信息',
        icon:'none'
      })
      return;
     }
 
     if(that.data.address=="请选择"){
      wx.showToast({
        title: '请选择使用地',
        icon:'none'
      })
      return;
    }
    var pys=that.data.yslist;
    var ptys={};
    for(var i=0;i<pys.length;i++){
      var arr={};
      if(pys[i].istian!=1){
        wx.showToast({
          title: '需要填写要素信息',
          icon:'none'
        })
        return;
      }else{
        arr.que={"id":pys[i].id,"name":pys[i].name};
        arr.ans= pys[i].tjs;
        ptys[i]=arr;
      }
    }
    var pdata={
       yt:that.data.yt.name,
       types:that.data.dtype.name,
       lqway:that.data.lqdata.name,
       lqpdata:JSON.stringify(that.data.lqtdata),
       syd:that.data.address,
       sqr:JSON.stringify(that.data.shiming),
       fen:that.data.fen,
       price:that.data.price,
       affair_id:that.data.sx.id,
       pys:JSON.stringify(ptys) 
    }
    console.log(pdata)
    ziliaop(pdata).then((res)=>{
      wx.showToast({
        title: res.msg,
        icon:'none'
      })
       if(res.code==1){
         console.log(res)
        setTimeout(() => {
          wx.navigateTo({
            url: "/pages/ziliaoup/ziliaoup?cid="+res.data,
          })
        }, 1000);
       }
    })
  }
})